################################################################################
#
#Copyright (c) 2011, Petr Skramovsky
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL Petr Skramovsky BE LIABLE FOR ANY
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################

from md3_io import MD3Reader
from bsp_io import BSPReader
from mmap import mmap
import os
from zipfile import ZipFile
from os.path import join
from shader import ShaderReader
    
class QFileSystem():
  def __init__(self, base):
    self.base = base
    self.path_baseq = join( self.base, "baseq3" )

    #TODO:mb some clearing...
    self.loaded_shaders = {}
    self.files = {}
    for f in os.listdir( self.path_baseq ):
      if f.endswith( ".pk3" ):
        p = ZipFile( join( self.path_baseq, f ), "r" )
        ns = p.namelist()
        for n in ns:
          self.files[ n ] = f
        p.close()

  def __getitem__(self,name):
    if not self.files.has_key( name ):
      return None

    path = self.files[ name ]
    pak = ZipFile( join( self.path_baseq, path ), "r" )
    file = pak.open( name )
    info = pak.getinfo( name )

    if name.endswith( ".shader" ):
      if self.loaded_shaders.has_key( name ):
        pak.close()
        return self.loaded_shaders[ name ]
      sh = ShaderReader().read( file )
      self.loaded_shaders[ name ] = sh
      pak.close()
      return sh
    elif name.endswith( ".bsp" ):
      mf = mmap(-1, info.file_size)
      for b in file.read():
        mf.write_byte( b )
      bsp = BSPReader().read( mf )
      mf.close()
      pak.close()
      return bsp
    elif name.endswith( ".md3" ):
      mf = mmap(-1, info.file_size)
      for b in file.read():
        mf.write_byte( b )
      md = MD3Reader().read( mf )
      mf.close()
      pak.close()
      return md
    else:
      data = file.read()
      pak.close()
      return data

  def extract( self, name, target ):
    if not self.files.has_key( name ):
      return None
    path = self.files[ name ]
    pak = ZipFile( join( self.path_baseq, path ), "r" )
    pak.extract( name, target )
    pak.close()
